package com.mindata.blockmanager.controller.admin;

import cn.hutool.core.bean.BeanUtil;
import com.google.common.base.Preconditions;
import com.mindata.blockmanager.bean.BaseData;
import com.mindata.blockmanager.bean.GridResultDto;
import com.mindata.blockmanager.bean.MemberGroupParam;
import com.mindata.blockmanager.bean.QueryPage;
import com.mindata.blockmanager.bean.ResultGenerator;
import com.mindata.blockmanager.manager.MemberGroupManager;
import com.mindata.blockmanager.manager.MemberManager;
import com.mindata.blockmanager.manager.PermissionManager;
import com.mindata.blockmanager.model.Member;
import com.mindata.blockmanager.model.MemberGroup;
import com.mindata.blockmanager.model.Permission;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.Date;

@Controller
@RequestMapping("/admin/group")
@Slf4j
public class GroupAdminController extends BaseController {

    @Resource
    private MemberGroupManager memberGroupManager;
    @Resource
    private MemberManager memberManager;
    @Resource
    private PermissionManager permissionManager;

    /**
     * 联盟查询.
     *
     * @return
     */
    @RequestMapping("/list")
    @ResponseBody
    public GridResultDto<MemberGroup> memberList(QueryPage page) {
        GridResultDto groupVos = memberGroupManager.pagingGroupList(page);
        return groupVos;
    }


    @RequestMapping("/add")
    @ResponseBody
    public BaseData addMember(MemberGroupParam param) {
        MemberGroup group = new MemberGroup();
        BeanUtil.copyProperties(param, group);
        group.setId(null);
        group.setCreateTime(new Date());
        group.setUpdateTime(new Date());
        memberGroupManager.addMemberGroup(group);
        return ResultGenerator.genSuccessResult();
    }

    @RequestMapping("/del")
    @ResponseBody
    public BaseData delGroup(long id) {
        //判断是否引用配置
        MemberGroup group = memberGroupManager.findById(id);
        if (group == null) {
            return ResultGenerator.genFailResult("error param");
        }
        Member member = memberManager.findFirstByGroupId(group.getGroupId());
        if (member != null) {
            return ResultGenerator.genFailResult("存在节点配置引用,无法删除");
        }

        Permission permission = permissionManager.findFirstByGroupId(group.getGroupId());
        if (permission != null) {
            return ResultGenerator.genFailResult("存在权限配置引用,无法删除");
        }

        memberGroupManager.delMemberGroup(id);
        return ResultGenerator.genSuccessResult();
    }

    @RequestMapping("/updatePage")
    public ModelAndView updatePage(long id) {
        Preconditions.checkState(id > 0, "error id");
        MemberGroup group = memberGroupManager.findById(id);
        Preconditions.checkNotNull(group, "error id");

        ModelAndView modelAndView = new ModelAndView("group/group_update");
        modelAndView.addObject("group", group);
        return modelAndView;
    }

    @RequestMapping("/update")
    @ResponseBody
    public BaseData update(MemberGroupParam param) {
        MemberGroup group = memberGroupManager.findById(param.getId());
        if (group == null) {
            return ResultGenerator.genFailResult("error param");
        }
        group.setName(param.getName());
        group.setCreateTime(new Date());
        group.setUpdateTime(new Date());
        memberGroupManager.updateMemberGroup(group);
        return ResultGenerator.genSuccessResult();
    }

}